package com.poi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.Bookmark;
import org.apache.poi.hwpf.usermodel.Bookmarks;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Section;

public class Test {

	public static void main111(String[] args) {
		try {
			InputStream is = new FileInputStream(new File("E:\\base\\a.doc"));
			WordExtractor ex = new WordExtractor(is);
			String text2003 = ex.getText();
			System.out.println(text2003);

			// OPCPackage opcPackage =
			// POIXMLDocument.openPackage("E:\\base\\model1.doc");
			// POIXMLTextExtractor extractor = new
			// XWPFWordExtractor(opcPackage);
			// String text2007 = extractor.getText();
			// System.out.println(text2007);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			String path1 = "E:\\base\\a.doc";
			InputStream is1 = new FileInputStream(path1);
			// HWPFDocument doc1 = new HWPFDocument(is1);
			WordExtractor ex = new WordExtractor(is1);

			String path = "E:\\base\\model1.doc";
			InputStream is = new FileInputStream(path);
			HWPFDocument doc = new HWPFDocument(is);

			// 输出书签信息
			// printInfo(doc.getBookmarks());
			// 输出文本
			// System.out.println(doc.getDocumentText());
			Range range = doc.getRange();
			Range r1 = new Range(doc.getBookmarks().getBookmark(1).getStart(),
					doc.getBookmarks().getBookmark(1).getEnd(), doc);
			r1.insertAfter(ex.getText());

			// r1.insertBefore("aaaaaaaassssssssssssss");

			// Range r2 = new
			// Range(doc.getBookmarks().getBookmark(1).getStart(),
			// doc.getBookmarks().getBookmark(1).getEnd(), doc);
			// r2.insertBefore("我们是中国人");
			doc.write(new FileOutputStream("E:\\base\\test.doc"));
			// this.insertInfo(range);
			// printInfo(r1);
			// 读表格
			// this.readTable(range);
			// 读列表
			// this.readList(range);
			// 删除range
			Range r = new Range(2, 5, doc);
			// r.delete();// 在内存中进行删除，如果需要保存到文件中需要再把它写回文件
			// 把当前HWPFDocument写到输出流中
			// doc.write(new FileOutputStream("D:\\test.doc"));
			closeStream(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void printInfo(Range range) {
		// 获取段落数
		int paraNum = range.numParagraphs();
		System.out.println(paraNum);
		for (int i = 0; i < paraNum; i++) {
			// this.insertInfo(range.getParagraph(i));
			System.out.println("段落" + (i + 1) + "："
					+ range.getParagraph(i).text());
			if (i == (paraNum - 1)) {
				// this.insertInfo(range.getParagraph(i));
			}
		}
		int secNum = range.numSections();
		System.out.println(secNum);
		Section section;
		for (int i = 0; i < secNum; i++) {
			section = range.getSection(i);
			System.out.println(section.getMarginLeft());
			System.out.println(section.getMarginRight());
			System.out.println(section.getMarginTop());
			System.out.println(section.getMarginBottom());
			System.out.println(section.getPageHeight());
			System.out.println(section.text());
		}
	}

	public static void printInfo(Bookmarks bookmarks) {
		int count = bookmarks.getBookmarksCount();
		System.out.println("书签数量：" + count);
		Bookmark bookmark;
		for (int i = 0; i < count; i++) {
			bookmark = bookmarks.getBookmark(i);
			System.out.println("书签" + (i + 1) + "的名称是：" + bookmark.getName());
			System.out.println("开始位置：" + bookmark.getStart());
			System.out.println("结束位置：" + bookmark.getEnd());
		}
	}

	public static void closeStream(InputStream is) {
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
